#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode. 
# export DH_VERBOSE=1

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	CMAKE_BUILD_TYPE=Debug
else
	CMAKE_BUILD_TYPE=Release
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

USER_CFLAGS=$(shell dpkg-buildflags --get CFLAGS)
USER_CXXFLAGS=$(shell dpkg-buildflags --get CXXFLAGS)

build: build-stamp
build-stamp:
	dh_testdir

	# backup orig makefile
	for makefile in `find ./ -name "Makefile"` ; do [ -f $${makefile}.orig ] || mv $${makefile} $${makefile}.orig ; done

	# Cmake based makefile
	cmake CMakeLists.txt \
		-DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE) \
		-DCMAKE_BUILD_STRIP=FALSE \
		-DFORCE_INTERNAL_SOUNDTOUCH=FALSE \
		-DUSER_CMAKE_C_FLAGS:STRING="$(USER_CFLAGS)" \
		-DUSER_CMAKE_CXX_FLAGS:STRING="$(USER_CXXFLAGS)"
	$(MAKE) $(MAKEFLAGS)

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	# Backup some orig makefile if it's not already done.
	# I hope that some will be delete by upstream when the cmake port is over.
	# Note: In case that we do not dl all the plugins, the test [ -f $${makefile} ] ensures it works
	for makefile in plugins/CDVDlinuz/Src/Linux/Makefile \
		plugins/CDVDiso/src/Linux/Makefile \
		plugins/CDVDiso/src/Windows/Makefile \
		plugins/USBnull/Windows/Makefile \
		plugins/FWnull/Windows/Makefile \
		plugins/PeopsSPU2/Makefile \
		plugins/CDVDisoEFP/src/Linux/Makefile ; do \
	[ -f $${makefile}.orig ] || ( [ -f $${makefile} ] && mv $${makefile} $${makefile}.orig ) || true ; done

	# Add here the commands to clean up after the build process.
	[ -f Makefile ] && $(MAKE) clean || true
	# Remove cmake stuff
	rm -fr $$(find . -type d -name CMakeFiles) 
	rm -f $$(find . -type f -name CMakeCache.txt)  $$(find . -type f -name cmake_install.cmake)
	rm -f $$(find . -type f -name Makefile)
	# Files generated by bin2cpp
	cd pcsx2/gui/Resources/ && rm -f App*.h Config*.h BackgroundLogo.h ButtonIcon_Camera.h Dualshock.h
	# leftover of cmake
	rm -f bin/plugins/ps2hw.dat
	rm -f pcsx2/svnrev.h
	# Restore orig makefile
	for makefile_orig in `find ./ -name "Makefile.orig"` ; do [ -f $${makefile_orig} ] && mv $${makefile_orig} `echo $${makefile_orig} | sed -e 's/.orig//'` ; done

	dh_clean

# Allow compilation when fglrx is installed
override_dh_shlibdeps:
	dh_shlibdeps -- --ignore-missing-info

override_dh_strip:
	dh_strip --package=pcsx2-unstable --dbg-package=pcsx2-unstable-dbg
	dh_strip --package=pcsx2-plugins-unstable --dbg-package=pcsx2-plugins-unstable-dbg

# Avoid to relaunch the compilation twice. (build and dh_auto_build target)
override_dh_auto_build:
	# Do nothing

%:
	dh $@ --parallel

.PHONY: build clean install
